package com.studentSystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.studentSystem.mapper.CourseMapper;
import com.studentSystem.pojo.Course;
import com.studentSystem.service.CourseService;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.SnowflakeIdWorkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageBeanUtil<Course> getAllCourse(Integer pageNum, Integer pageSize, Long id, String name, Long teacherId) {
        int defaultPageNum = 1;
        int defaultPageSize = 5;

        if (pageNum == null || pageNum <= 0) {
            pageNum = defaultPageNum;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = defaultPageSize;
        }
        PageHelper.startPage(pageNum, pageSize);

        List<Course> courses;
        courses = courseMapper.list(id, name, teacherId);

        PageInfo<Course> pageInfo = new PageInfo<>(courses);

        PageBeanUtil<Course> pageBeanUtil = new PageBeanUtil<>();
        pageBeanUtil.setTotal(pageInfo.getTotal());
        pageBeanUtil.setItems(pageInfo.getList());

        return pageBeanUtil;
    }

    @Override
    public void delete(Long id) {
        courseMapper.delete(id);
    }

    @Override
    public void add(Course course) {
        Long id = new SnowflakeIdWorkerUtil(0,0).nextId();
        course.setId(id);
        courseMapper.add(course);
    }

    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }

    @Override
    public Course getById(Long id) {
        return courseMapper.getById(id);
    }

    @Override
    public List<Course> getAllCourses() {
        return courseMapper.list(null, null, null);
    }
}
